package com.carkeeper.blog.controller;

import com.carkeeper.blog.pojo.Blog;
import com.carkeeper.blog.service.BlogService;
import com.carkeeper.entity.Result;
import com.carkeeper.entity.StatusCode;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;

/**
 * @program: carkeeper_parent
 * @Date: 2020/11/23 14:16
 * @Author: qgg
 * @Description:
 */
@RestController
@RequestMapping("/blog")
@Api(tags = "推文管理接口api")
public class BlogController {

    @Autowired
    private BlogService blogService;


    /**
     * 查询所有
     * @return
     */
    @GetMapping("/findAll")
    @ApiOperation("查询列表接口")
    private Result findAll(){
        List<Blog> blogList = blogService.findAll();
        return new Result(true , StatusCode.OK , "查询成功" , blogList);
    }

    //新增
    @PostMapping("/add")
    @ApiOperation("新增接口")
    public Result add(@RequestBody Blog blog){
        blogService.add(blog);
        return new Result(true , StatusCode.OK , "添加成功");
    }

    //删除
    @DeleteMapping("/{id}")
    @ApiOperation("删除接口")
    public Result delete(@PathVariable("id") String id){
        blogService.delete(id);
        return new Result(true , StatusCode.OK , "删除成功");
    }

    //修改
    @PutMapping("/update")
    @ApiOperation("修改接口")
    public Result update(@RequestBody Blog blog){
        blogService.update(blog);
        return new Result(true , StatusCode.OK , "修改成功");
    }

    //根据id查询数据
    @GetMapping("/findById/{id}")
    @ApiOperation("通过id查询列表接口")
    public Result findById(@PathVariable("id") String id){
        Blog blog = blogService.findById(id);
        return new Result(true , StatusCode.OK , "查询成功" , blog);
    }

    //多条件查询
    @GetMapping("/search")
    @ApiImplicitParam(name = "searchMap",required = false)
    public Result findList(@RequestParam Map searchMap){
        System.out.println("searchMap = " + searchMap);
        List<Blog> list = blogService.findList(searchMap);
        System.out.println("list = " + list);
        return new Result(true , StatusCode.OK , "查询成功" , list);
    }

    //分页
    @GetMapping("/{pageNum}/{pageSize}")
    @ApiOperation("分页查询列表接口")
    public Result findPage(@PathVariable("pageNum") int pageNum , @PathVariable("pageSize") int pageSize){
        PageInfo page = blogService.findPage(pageNum, pageSize);
        return new Result(true , StatusCode.OK , "查询成功" , page);
    }

    //修改状态
    @PutMapping("/updateStatus/{id}")
    @ApiOperation("修改状态接口")
    public Result updateStatus(@PathVariable("id") String id){
        blogService.updateStatus(id);
        return new Result(true , StatusCode.OK , "修改状态成功");
    }
}
